﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Configuration;
using DAL;
using Common;


public partial class Customer_SelectContract : System.Web.UI.Page
{   
    
    protected string MenuType
    {
        get { return string.IsNullOrEmpty(Request["menutype"]) ? "" : Request["menutype"].ToString(); }
    }

    protected string ParentID;

    protected string Status;

    protected void Page_PreRender(object sender, EventArgs e)
    {
            ParentID = string.IsNullOrEmpty(Request["ParentID"]) ? "" : Request["ParentID"].ToString();
            Status = string.IsNullOrEmpty(Request["Status"]) ? "" : Request["Status"].ToString();

            OneDate.Attributes.Add("onclick","selectDate(this);");
            TwoDate.Attributes.Add("onclick", "selectDate(this);"); 
            
            //条件 Hashtable

            Hashtable tb = new Hashtable();
            bool QueryAdd = false;

            if (MenuType != "")
            {
                tb.Add("MenuType", MenuType);
                QueryAdd = true;
            }

            if (Status != "")
            {
                tb.Add("ExpireDay", 15);
                QueryAdd = true;
            }

            if (!string.IsNullOrEmpty(CustName.Text.Trim()))
            {
                tb.Add("CustName", CustName.Text.Trim());
                QueryAdd = true;
            }
            if (!string.IsNullOrEmpty(Keyword.Text.Trim()))
            {
                tb.Add("Keyword", Keyword.Text.Trim());
                QueryAdd = true;
            }
            if (OneDate.Text.Trim() != "选择开始日期")
            {
                tb.Add("OneDate", OneDate.Text.Trim());
                QueryAdd = true;
            }
            if (TwoDate.Text.Trim() != "选择结束日期")
            {
                tb.Add("TwoDate", TwoDate.Text.Trim());
                QueryAdd = true;
            }
            tb = QueryAdd ? tb : null;

            ContractDataBind(tb);
    }

    private void ContractDataBind(Hashtable QueryTable)
    {
        lv_Contract.DataSource = new Contract().SelectContract(QueryTable); 
        lv_Contract.DataBind();
    }

    //搜索
    protected void submit_Click(object sender, EventArgs e)
    {
        Hashtable tb = new Hashtable();
        bool QueryAdd = false;

        //条件 Hashtable

        if (!string.IsNullOrEmpty(CustName.Text.Trim()))
        {
            tb.Add("StName", CustName.Text.Trim());
            QueryAdd = true;
        }
        if (!string.IsNullOrEmpty(Keyword.Text.Trim()))
        {
            tb.Add("Keyword", Keyword.Text.Trim());
            QueryAdd = true;
        }
        if (OneDate.Text.Trim() != "选择开始日期")
        {
            tb.Add("OneDate", OneDate.Text.Trim());
            QueryAdd = true;
        }
        if (TwoDate.Text.Trim() != "选择结束日期")
        {
            tb.Add("TwoDate", TwoDate.Text.Trim());
            QueryAdd = true;
        }
        tb = QueryAdd ? tb : null;

        ContractDataBind(tb);
    }


   
    //DropDownList分页选择
    protected void PageList_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList pagelist = (DropDownList)sender;

        int CurPage = Convert.ToInt32(pagelist.SelectedValue);
        int TotalPage = WebUtility.GetTotalPage(DataPager1.TotalRowCount, DataPager1.PageSize);

        DataPager1.SetPageProperties((CurPage - 1) * DataPager1.PageSize, DataPager1.PageSize, true);
    }

    protected void lv_Contract_DataBound(object sender, EventArgs e)
    {
        DropDownList pagelist = (DropDownList)DataPager1.Controls[4].FindControl("PageList");

        int CurPage = WebUtility.GetCurrentPage(DataPager1.StartRowIndex, DataPager1.PageSize);
        int TotalPage = WebUtility.GetTotalPage(DataPager1.TotalRowCount, DataPager1.PageSize);
        for (int i = 1; i <= TotalPage; i++)
            pagelist.Items.Add(new ListItem("第" + i.ToString() + "页", i.ToString()));
        pagelist.SelectedValue = CurPage.ToString();
    }
    protected void lv_Contract_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        new Contract().DelContract(Convert.ToInt32(lv_Contract.DataKeys[e.ItemIndex].Value));
    }

    //Export
    protected void submit0_Click(object sender, EventArgs e)
    {
        //条件 Hashtable

        Hashtable tb = new Hashtable();
        bool QueryAdd = false;

        if (!string.IsNullOrEmpty(CustName.Text.Trim()))
        {
            tb.Add("StName", CustName.Text.Trim());
            QueryAdd = true;
        }
        if (!string.IsNullOrEmpty(Keyword.Text.Trim()))
        {
            tb.Add("Keyword", Keyword.Text.Trim());
            QueryAdd = true;
        }
        if (OneDate.Text.Trim() != "选择开始日期")
        {
            tb.Add("OneDate", OneDate.Text.Trim());
            QueryAdd = true;
        }
        if (TwoDate.Text.Trim() != "选择结束日期")
        {
            tb.Add("TwoDate", TwoDate.Text.Trim());
            QueryAdd = true;
        }
        tb = QueryAdd ? tb : null;

        Contract.DataToExcel(tb);
    }
}
